package com.zucai365.football.action;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ActionSupport;
import com.zucai365.football.bean.LianSaiBean;
import com.zucai365.football.bean.LianSaiInfoBean;
import com.zucai365.football.bean.LianSaiYearBean;
import com.zucai365.football.service.CurrentFootballTeamService;
import com.zucai365.football.service.FootballTeamService;
import com.zucai365.football.service.LianSaiInfoService;
import com.zucai365.football.service.LianSaiService;
import com.zucai365.football.service.LianSaiYearService;
import com.zucai365.football.service.TeamDZService;
import com.zucai365.football.service.TeamPlayerService;
import com.zucai365.football.utils.Constant;
import com.zucai365.football.utils.ZuCaiUtils;

public class LianSaiAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 324131915174506299L;
	
	@Autowired
	@Qualifier("lianSaiService")
	private LianSaiService lianSaiService;

	public LianSaiService getLianSaiService() {
		return lianSaiService;
	}

	public void setLianSaiService(LianSaiService lianSaiService) {
		this.lianSaiService = lianSaiService;
	}
	
	@Autowired
	@Qualifier("lianSaiInfoService")
	private LianSaiInfoService lianSaiInfoService;
	
	public LianSaiInfoService getLianSaiInfoService() {
		return lianSaiInfoService;
	}

	public void setLianSaiInfoService(LianSaiInfoService lianSaiInfoService) {
		this.lianSaiInfoService = lianSaiInfoService;
	}
	
	@Autowired
	@Qualifier("lianSaiYearService")
	private LianSaiYearService lianSaiYearService;
	
	public LianSaiYearService getLianSaiYearService() {
		return lianSaiYearService;
	}

	public void setLianSaiYearService(LianSaiYearService lianSaiYearService) {
		this.lianSaiYearService = lianSaiYearService;
	}
	
	@Autowired
	@Qualifier("footballTeamService")
	private FootballTeamService footballTeamService;
	
	public FootballTeamService getFootballTeamService() {
		return footballTeamService;
	}

	public void setFootballTeamService(FootballTeamService footballTeamService) {
		this.footballTeamService = footballTeamService;
	}
	
	@Autowired
	@Qualifier("teamDZService")
	private TeamDZService teamDZService;
	
	public TeamDZService getTeamDZService() {
		return teamDZService;
	}

	public void setTeamDZService(TeamDZService teamDZService) {
		this.teamDZService = teamDZService;
	}

	/**
	 * 当前赛季ID
	 */
	private Long lianSaiYearId;
	
	public Long getLianSaiYearId() {
		return lianSaiYearId;
	}

	public void setLianSaiYearId( Long lianSaiYearId ) {
		this.lianSaiYearId = lianSaiYearId;
	}
	
	/**
	 * 当前赛季信息ID
	 */
	private Long lianSaiInfoId;
	
	public Long getLianSaiInfoId() {
		return lianSaiInfoId;
	}

	public void setLianSaiInfoId(Long lianSaiInfoId) {
		this.lianSaiInfoId = lianSaiInfoId;
	}

	public String list(){
		
		ServletActionContext.getRequest().setAttribute( "lianSaiYearList", Constant.ALL_LIANSAI_YEAR_LIST );
		
		if( this.lianSaiYearId == null ){
			lianSaiYearId = ZuCaiUtils.CURRENT_LIANSAI_YEAR;
		}
		//查询一个赛季的所有联赛记录
		List<LianSaiInfoBean> lianSaiInfoList = this.lianSaiInfoService.getLianSaiInfo( null,  this.lianSaiYearId );
		
		if( lianSaiInfoList.isEmpty()  ){
			ServletActionContext.getRequest().setAttribute( "lianSaiList", Constant.ALL_LIANSAI_LIST );
			ServletActionContext.getRequest().setAttribute( "flag", "1" );
		}else{
			ServletActionContext.getRequest().setAttribute( "lianSaiInfoList", lianSaiInfoList );
			ServletActionContext.getRequest().setAttribute( "flag", "2" );
		}
			
		return "list";
	}
	
	
	/**
	 * 保存每个联赛的球队数据
	 * @return
	 */
	public String save(){
		
		
		List<LianSaiInfoBean> lianSaiInfoList = new ArrayList<LianSaiInfoBean>();
		
		Iterator<String> attNames = ServletActionContext.getRequest().getParameterMap().keySet().iterator();
		while( attNames.hasNext() ){
			String name = attNames.next();
			if( name.startsWith( "id_" ) ){
				String lianSaiId = name.split( "_" )[1];
				
				LianSaiBean lianSai = this.lianSaiService.get( Long.valueOf( lianSaiId ) );				
				LianSaiYearBean lianSaiYear = this.lianSaiYearService.get( Long.valueOf( this.lianSaiYearId ) );
				
				LianSaiInfoBean lianSaiInfo = new LianSaiInfoBean();
				lianSaiInfo.setLianSai(lianSai);
				lianSaiInfo.setLianSaiYear(lianSaiYear);
				lianSaiInfo.setChineseName( lianSai.getChineseName() );
				
				String url_bet007 = ServletActionContext.getRequest().getParameter( "bet007_url_" + lianSaiId );
				
				lianSaiInfo.setUrlBet007( null == url_bet007 ? null : url_bet007.trim()  );
				
				lianSaiInfoList.add( lianSaiInfo );
			}
		}
		
		try{
			this.lianSaiInfoService.saveAll( lianSaiInfoList );
		}catch( Exception ex ){
			ex.printStackTrace();
			throw new RuntimeException( ex );
		}
		
		ZuCaiUtils.dowloadTeamJs( lianSaiInfoList );
		
		return "save";
	}
	
	
	/**
	 * 更新每个联赛的球队数据
	 * @return
	 */
	public String update(){
		
		
		List<LianSaiInfoBean> lianSaiInfoList = new ArrayList<LianSaiInfoBean>();
		
		Iterator<String> attNames = ServletActionContext.getRequest().getParameterMap().keySet().iterator();
		while( attNames.hasNext() ){
			String name = attNames.next();
			if( name.startsWith( "id_" ) ){
				String lianSaiInfoId = name.split( "_" )[1];
				
				String url_bet007 = ServletActionContext.getRequest().getParameter( "bet007_url_" + lianSaiInfoId );
				LianSaiInfoBean lianSaiInfo = this.lianSaiInfoService.get( Long.valueOf( lianSaiInfoId ) );
				
				String lunci = ServletActionContext.getRequest().getParameter( "lunci_" + lianSaiInfoId );
				
				lianSaiInfo.setUrlBet007( null == url_bet007 ? null : url_bet007.trim() );
				lianSaiInfo.setLunci( Integer.parseInt( lunci ) );
				
				lianSaiInfoList.add( lianSaiInfo );
			}
		}
		
		try{
			this.lianSaiInfoService.saveAll( lianSaiInfoList );
		}catch( Exception ex ){
			ex.printStackTrace();
			throw new RuntimeException( ex );
		}
		
		Constant.ALL_LIANSAI_INFO_LIST.clear();
		Constant.ALL_LIANSAI_INFO_LIST = this.lianSaiInfoService.findAll();
		
		ZuCaiUtils.dowloadTeamJs( lianSaiInfoList );
		
		return "save";
	}
	
	/**
	 * 生成一个赛季的球队列表
	 * @return
	 */
	public String saveFootballTeam(){
		
		LianSaiInfoBean lianSaiInfo = this.lianSaiInfoService.get( lianSaiInfoId );
		
		String jspath = Constant.WAR_PATH + "teamInfo/" + lianSaiInfo.getLianSai().getEnglishName() + "/" + lianSaiInfo.getLianSaiYear().getLiansaiYear() + ".js" ;
		
		File jsFile = new File( jspath );
		FileInputStream stream = null;
		InputStreamReader reader = null;
		BufferedReader br = null;
		//球队json字符串
		String teamJson = "";
		
		try {
			stream = new FileInputStream( jsFile );
			reader = new InputStreamReader( stream, "utf-8" );
			br = new BufferedReader( reader );		
			
			String s = "";
			while (( s = br.readLine()) != null ) {			
				if( s.startsWith( "var arrTeam" )){
					teamJson = s.split( "=" )[1].trim() ;
					teamJson = teamJson.substring( 0, teamJson.length()-1 );
					break;
				}
			}
			
			this.footballTeamService.saveTeam( lianSaiInfo , teamJson );
			
		} catch ( Exception e) {
			throw new RuntimeException( e.getCause() );
		}finally{
			try {
				stream.close();
				reader.close();
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return "save";
	}
	
	
	/**
	 * 生成一个赛季的球队对阵关系
	 * @return
	 */
	public String saveFootballTeamDZ(){
		
		LianSaiInfoBean lianSaiInfo = this.lianSaiInfoService.get( lianSaiInfoId );
		
		String jspath = Constant.WAR_PATH + "teamInfo/" + lianSaiInfo.getLianSai().getEnglishName() + "/" + lianSaiInfo.getLianSaiYear().getLiansaiYear() + ".js" ;
		
		File jsFile = new File( jspath );
		FileInputStream stream = null;
		InputStreamReader reader = null;
		BufferedReader br = null;
		
		//球队对阵json字符串
		List<String> teamDzList = new ArrayList<String>();
		
		try {
			stream = new FileInputStream( jsFile );
			reader = new InputStreamReader( stream, "utf-8" );
			br = new BufferedReader( reader );		
			
			String teamDZJson = "";
			while (( teamDZJson = br.readLine()) != null ) {			
				if( teamDZJson.startsWith( "jh" )){					
					teamDzList.add( teamDZJson );
				}
			}
			
			this.teamDZService.saveTeamDZ( lianSaiInfo.getLianSai(), lianSaiInfo.getLianSaiYear(), teamDzList );
			
		} catch ( Exception e) {
			throw new RuntimeException( e.getCause() );
		}finally{
			try {
				stream.close();
				reader.close();
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return "save";
		
	}
	
	@Autowired
	@Qualifier("currentFootballTeamService")
	private CurrentFootballTeamService currentFootballTeamService;
	
	public CurrentFootballTeamService getCurrentFootballTeamService() {
		return currentFootballTeamService;
	}

	public void setCurrentFootballTeamService(
			CurrentFootballTeamService currentFootballTeamService) {
		this.currentFootballTeamService = currentFootballTeamService;
	}
	
	private Long lianSaiId;
	
	public Long getLianSaiId() {
		return lianSaiId;
	}

	public void setLianSaiId(Long lianSaiId) {
		this.lianSaiId = lianSaiId;
	}

	/**
	 * 统计球队的积分
	 * @return
	 */
	public String tjJifen(){
		
		this.currentFootballTeamService.saveFootballTeamJiFen(lianSaiId, lianSaiYearId);
		
		return "save";
		
	}
	
	/**
	 * 统计进球
	 * @return
	 */
	public String tjJinQiu(){
		
		this.currentFootballTeamService.saveFootballTeamJinQiu(lianSaiId, lianSaiYearId);
		
		return "save";		
	}
	
	@Autowired
	@Qualifier("teamPlayerService")
	private TeamPlayerService teamPlayerService;
	
	public TeamPlayerService getTeamPlayerService() {
		return teamPlayerService;
	}

	public void setTeamPlayerService(TeamPlayerService teamPlayerService) {
		this.teamPlayerService = teamPlayerService;
	}

	/**
	 * 统计球员上场时间
	 * @return
	 */
	public String tjZhuLi(){
		
		this.teamPlayerService.saveTeamPlayerPlayTime(lianSaiId, lianSaiYearId);
		
		return "save";		
	}
	
	/**
	 * 统计红黄牌数量
	 * @return
	 */
	public String tjHongHuangPai(){
		
		this.teamPlayerService.saveTeamPlayerHongHuangPai(lianSaiId, lianSaiYearId);
		
		return "save";
	}
	
	/**
	 * 统计所有数据
	 * @return
	 */
	public String tjAllData(){
		//积分统计
		this.currentFootballTeamService.saveFootballTeamJiFen(lianSaiId, lianSaiYearId);
		//上场时间统计
		this.teamPlayerService.saveTeamPlayerPlayTime(lianSaiId, lianSaiYearId);
		//红黄牌统计
		this.teamPlayerService.saveTeamPlayerHongHuangPai(lianSaiId, lianSaiYearId);
		//进球统计
		this.currentFootballTeamService.saveFootballTeamJinQiu(lianSaiId, lianSaiYearId);	
		return "save";		
	}
	
}
